package com.mk.seckill.vo;

import com.mk.seckill.utils.ValidatorUtil;
import com.mk.seckill.validator.IsMobile;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
 * @Author hhwu
 * @Description //TODO 手机号码校验规则，为validator包中的IsMobile注解服务
 * @Date 20:44 2022/6/6
 **/
public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {// 泛型中指定为哪个注解服务

	private boolean required = false;

	@Override
	public void initialize(IsMobile constraintAnnotation) {
		required = constraintAnnotation.required();
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		/**
		 * @Author hhwu
		 * @Description //TODO 校验规则主体
		 * @Date 20:58 2022/6/6
		 * @Param [value, context]
		 * @return boolean
		 **/
		if (required){ // 如果手机号码必填，则进行手机号码校验
			return ValidatorUtil.isMobile(value);// 判断是否是手机号
		}else {
			if (StringUtils.isEmpty(value)){
				return true;
			}else {
				return ValidatorUtil.isMobile(value);
			}
		}
	}
}